package com.adds.lvds.security.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adds.lvds.core.plugin.PageView;
import com.adds.lvds.core.trace.TraceContent;
import com.adds.lvds.core.trace.TraceService;
import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.dao.impl.organize.SysUserInfoMapper;
import com.adds.lvds.dao.impl.organize.SysUserPermissionMapper;
import com.adds.lvds.model.organize.SysUserInfo;
import com.adds.lvds.model.organize.SysUserPermission;
import com.adds.lvds.security.dao.SysRolesMapper;
import com.adds.lvds.security.dao.SysUserMapper;
import com.adds.lvds.security.model.SysCurrentUser;
import com.adds.lvds.security.model.SysRoleUser;
import com.adds.lvds.service.impl.organize.SysUserPermissionService;

@Transactional(rollbackFor = Exception.class)
@Service("sysUserService")
public class SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysUserInfoMapper sysUserInfoMapper;
	@Autowired
	private SysRolesMapper roleMapper;
	@Autowired
	private SysUserPermissionService sysUserPermissionService;
	@Autowired
	private SysUserPermissionMapper sysUserPermissionMapper;

	/**
	 * 返回所有数据
	 */
	public PageView<SysCurrentUser> query(PageView<SysCurrentUser> pageView,
			SysCurrentUser t) {
		List<SysCurrentUser> list = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("paging", pageView);
			map.put("t", t);
			list = sysUserMapper.pageSysUserByCondition(map);
			pageView.setRecords(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageView;
	}

	/**
	 * 根据id获取用户信息
	 */
	public SysCurrentUser getSysUserByMap(Map<String, Object> map) {
		SysCurrentUser sysCurrentUser = null;
		try {
			sysCurrentUser = this.sysUserMapper.getSysUserByMap(map);
		} catch (Exception e) {
			TraceService.error(TraceContent.SYS_USER,
					"[查看用户]-[message]" + e.getMessage());
			e.printStackTrace();
		}

		return sysCurrentUser;
	}

	/**
	 * 添加
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String addSysUser(SysCurrentUser sysUser, SysUserInfo sysUserInfo,
			SysRoleUser sysRoleUser, SysUserPermission perm) {
		String result = CommUtils.SUCC;
		try {
			sysUserMapper.addSysUser(sysUser);
			perm.setUser_id(sysUser.getId());
			perm.setType("1");
			sysUserPermissionMapper.insertSysUserPermission(perm);
			sysUserInfo.setUser_id(sysUser.getId());
			// 绑定员工数据到userInfo 表
			sysUserInfoMapper.updateUserInfo(sysUserInfo);
			roleMapper.deleteUserRole(sysUser.getId() + "");
			sysRoleUser.setUserId(sysUser.getId());
			roleMapper.addUserRole(sysRoleUser);
			TraceService.info(TraceContent.SYS_USER,
					"[录入用户]-[用户id]-" + sysUser.getId() + " 成功");
		} catch (Exception e) {
			TraceService.error(TraceContent.SYS_USER, "[录入用户]-[用户id]-"
					+ sysUser.getId() + "[message]" + e.getMessage());
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}

	/**
	 * 根据id修改
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String updateSysUser(SysCurrentUser sysUser,
			SysRoleUser sysRoleUser, SysUserPermission perm) {
		String result = CommUtils.SUCC;
		try {
			// 编辑用户账户数据
			sysUserMapper.updateSysUser(sysUser);
			sysUserPermissionService.deletePermissionByUserId(sysUser.getId());
			perm.setUser_id(sysUser.getId());
			perm.setType("1");
			sysUserPermissionService.addSysUserPermissions(perm);
			// 编辑角色数据
			roleMapper.deleteUserRole(sysUser.getId() + "");
			roleMapper.addUserRole(sysRoleUser);
			TraceService.info(TraceContent.SYS_USER,
					"[编辑用户]-[用户id]-" + sysUser.getId() + " 成功");
		} catch (Exception e) {
			TraceService.error(TraceContent.SYS_USER, "[编辑用户]-[用户id]-"
					+ sysUser.getId() + "[message]" + e.getMessage());
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}

	/**
	 * 根据Id批量删除数据
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String deleteSysUser(String[] ids) {

		String result = CommUtils.SUCC;
		try {
			// 删除用户数据
			sysUserMapper.batchDelete(ids);
			roleMapper.batchUserRole(ids);
			sysUserPermissionMapper.batchPermissionByUserId(ids);
			sysUserInfoMapper.updateUserInfoByMap(ids);
			TraceService.info(TraceContent.SYS_USER, "[删除用户]-[用户id]-" + ids
					+ " 成功");
		} catch (Exception e) {
			TraceService.error(TraceContent.SYS_USER, "[删除用户]-[用户id]-" + ids
					+ "[异常]" + e.getMessage());
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}

	/**
	 * 根据id修改
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String editSysUserByMap(SysCurrentUser sysUser) {
		String result = CommUtils.SUCC;
		try {
			// 录入用户账户数据
			sysUserMapper.updateSysUser(sysUser);
			TraceService.info(TraceContent.SYS_USER,
					"[编辑用户]-[用户id]-" + sysUser.getId() + " 成功");
		} catch (Exception e) {
			TraceService.error(TraceContent.SYS_USER, "[编辑用户]-[用户id]-"
					+ sysUser.getId() + "[message]" + e.getMessage());
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}
}
